<template>
	<view class="task-wrap">
		<view class="scroll-wrap" style="position: relative;">
			<view class="tabContainer" style="margin-bottom: 120px;">
				<!-- 样品列表 -->
				<!-- <template v-for="(item, index) in sampleTaskInfo.sampleItemVos">
					<sampleFactor></sampleFactor>
				</template> -->
				<template>
					<view class="sample-term-wrap">
						<view class="top_head">
							<view class="top_head_nav">
								<b style="width: 80px;">样品编号</b>
								<view class="content">{{code}}</view>
							</view>
							<view class="top_head_nav" style="margin-top: 8px;">
								<b style="width: 80px;">检测因子</b>
								<view class="content">{{factors}}</view>
							</view>
						</view>

						<view class="sample-term">
							<view class="center_between">
								<b style="width: 80px;">点位</b>
								<view class="content center_between">
									<view class="content" style="margin-right: 10px;text-align: left;">

										<input type="text" v-model="addressVal">
									</view>
									<u-icon name="edit-pen-fill" color="#193566" size="32" />
								</view>
							</view>
						</view>

						<view style="display: flex;justify-content: space-between;">
							<view class="sample-term" style="flex: 1;margin-right: 20px;text-align: center;"
								@click="addFactor">
								<b style="color: #193566;font-weight: 700;">新增因子</b>
							</view>
							<view class="sample-term" style="flex: 1;text-align: center;" @click="addFactorCombination">
								<b style="color: #193566;font-weight: 700;">新增同系因子套餐</b>
							</view>
						</view>

						<view class="sample-term" style="position: relative;"
							v-for="(item, index) in list">
							<view class="" v-if="item.isFactor == 0">

								<view class="center_between">
									<b style="width: 80px;font-weight: 700;">检测因子</b>
									<view class="content center_between">
										<b class="content"
											style="margin-right: 10px;text-align: left;color: #193566;font-weight: 700;">
											{{item.factorName}}
										</b>
									</view>
								</view>


								<view class="center_between" style="margin: 8px 0;">
									<view style="width: 80px;font-size: 12px;">标准号</view>
									<view class="content center_between">
										<view class="content" style="margin-right: 10px;text-align: left;">
											{{item.standardNo}}
										</view>
									</view>
								</view>
								<view class="center_between" style="margin:0 0 8px 0;">
									<view style="width: 80px;font-size: 12px;">标准名称</view>
									<view class="content center_between">
										<view class="content" style="margin-right: 10px;text-align: left;">
											{{item.standardName}}
										</view>
									</view>
								</view>
								<view class="center_between">
									<view style="width: 80px;font-size: 12px;">分析方法</view>
									<view class="content center_between">
										<view class="content" style="margin-right: 10px;text-align: left;">
											{{item.analysisMethod}}
										</view>
									</view>
								</view>

							</view>

							<view class="" v-else>
								<view class="center_between" style="margin: 8px 0;">
									<b style="width: 80px;font-size: 12px;font-weight: 700;">套餐名称</b>
									<view class="content center_between">
										<view class="content"
											style="margin-right: 10px;text-align: left;font-weight: 700;color: #193566;">
											{{item.factorName}}
										</view>
									</view>
								</view>

								<view class="center_between" style="margin: 8px 0;">
									<view style="width: 80px;font-size: 12px;">检测因子</view>
									<view class="content center_between">
										<view class="content" style="margin-right: 10px;text-align: left;">
											{{item.name.toString()}}
										</view>
									</view>
								</view>
							</view>
							<view class="nav_btn" style="z-index: 0;">
								<button type="warning " class="dialogBtn navSingleBtn" style='color:#fff'
									@click="del(item,index)">删除</button>
							</view>
						</view>

						<u-empty v-if="list.length == 0" style="margin-top:50px"></u-empty>


					</view>
				</template>
			</view>

			<view class="footerBtn" style="display: flex;">
				<view class="btn footerBtn footerBtns">
					<view style="padding:0 10px;flex: 1;">
						<button class="rightTabsBtn rightTabsSingleBtn" type="primary" @click="commit"
							:loading="commitLoading" style="color: #fff;">提交</button>
					</view>

				</view>

			</view>
		</view>
	</view>
</template>

<script>
	import sampleFactor from '@/components/sample/sample-factor.vue'
	export default {
		components: {
			sampleFactor,
		},
		data() {
			return {
				code: '',
				factors: '',
				sampsIdArr: [], //选择改修的样品id数组
				list: [],
				addressVal: '',
				secdClassId: '',

				isEgManage: false,
				checkboxVisible: true,
				commitLoading: false,
				taskId: '',
				sampleTaskInfo: {
					projectName: '',
					consignorName: '',
					inspectionName: '',
					sampleCount: '',
					jobId: '',
					inspectionLinker: '',
					singImageUrl: '',
					sampleVos: [],
				},
				editArr: [], //选中的修改样品
			}
		},
		onLoad(e) {
			console.log(e.editArr,636);
			if (e.taskId) this.taskId = e.taskId
			if (e.editArr) {
				this.editArr = JSON.parse(decodeURIComponent(e.editArr))
				
					console.log(this.editArr ,636);
			}
		},
		mounted() {
			let codeArr = []
			let factorsArr = []
			this.editArr.map((item) => {
				codeArr.push(item.sampleNo)
				factorsArr.push(item.factorName)
				this.sampsIdArr.push(item.sampItemId)
			})
			this.addressVal = this.editArr[0].factorPoint || ''
			this.secdClassId = this.editArr[0].secdClassId || ''
			this.code = codeArr.toString()
			this.factors = factorsArr.toString()


			uni.$on('query', (query) => {
				let data = [...this.list, ...query.paramsName]
				let map = new Map();
				for (let item of data) {
					if (!map.has(item.standardCode)) {
						map.set(item.standardCode, item);
					};
				};
				this.list = [...map.values()];
			});
			let _this = this
			uni.$on('edit_querys', (query) => {
				let data = [...this.list, ...query.params]
				let map = new Map();
				for (let item of data) {
					if (!map.has(item.standardCode)) {
						map.set(item.standardCode, item);
					};
				};
				this.list = [...map.values()];
			});
			this.querys()

		},
		destroyed() {
			uni.$off('query')
		},
		methods: {

			//查询批量修改样品-因子列表
			querys() {
				let params = {
					sampItemIds: this.sampsIdArr.toString()
				}
				this.$api.commonForGetParam('sample/factorList', params, res => {
					if (res.code == 200) {
						res.result.map((item) => {
							item.standardCode = item.checkStandardId
							if (item.isFactor == 1) {
									let name=item.factorName.split('(')[0]
									let tag= item.factorName.substr(
									     item.factorName.indexOf('('),
									     item.factorName.indexOf(')')
									   ).replace(/^\(+/,"").replace(/\)+$/,"")
									   item.factorName = tag
									   item.name = name
							} else {
							}
						})
						this.list = res.result
						this.list.map((item) => {
							item.standardCode = item.checkStandardId
						})
					}
				})
			},
			//新增因子
			addFactor() {
				console.log('新增因子');
				let dataInfo = {
					code: this.code,
					factors: this.factors,
					addressVal: this.addressVal,
					secdClassId: this.secdClassId,
				}
				this.$common.navigateTo('/pages/sampleLeader/opt/factorOpt/addFactor?source=' + 'edit_addFactor' +
					'&dataInfo=' + encodeURIComponent(JSON.stringify(dataInfo)));
			},
			//新增同系因子套餐
			addFactorCombination() {
				console.log('新增同系因子套餐');
				this.$common.navigateTo('/pages/sampleLeader/opt/factorOpt/addFactorCombination');
			},
			//删除
			del(item, index) {
				this.list.splice(index, 1)
			},
			//提交
			commit() {
				this.commitLoading = true
				if (this.list.length == 0) {
					uni.showToast({
						title: '请添加因子',
						icon: 'error',
						duration: 2000
					});
					this.commitLoading = false
					return
				}
				if (this.addressVal == '') {
					uni.showToast({
						title: '点位不能为空',
						icon: 'error',
						duration: 2000
					});
					this.commitLoading = false
					return
				}

				let oldFatactor = []
				let oldGroup = []
				// let oldGroupArr = ''
				let params = {
					sampItemIds: this.sampsIdArr.toString()
				}
				this.$api.commonForGetParam('sample/factorList', params, res => {
					if (res.code == 200) {
						res.result.map((item) => {
							if (item.isFactor == 0) {
								oldFatactor.push(item.checkStandardId)
							} else {
								oldGroup.push(item.checkStandardId)
							}

						})
					}
					
					let allGroupArr = []
					let allGroupIds = []
					let allFatactorIds = []
					console.log(this.list, 'this.list');
					this.list.map((item) => {
						if (item.isFactor == 0) {
							allFatactorIds.push(item.standardCode)
						} else {
							allGroupArr.push(item)
							allGroupIds.push(item.standardCode)
						}
					})
					// 原数组  oldFatactor  oldGroup
					// 现有所有数组 this.list
					let deleteMethodIds = [] //删除因子ID列表
					let addMethodIds = [] //新增因子ID列表


					let deleteGroupIds = [] //删除因子套餐ID列表
					let groups = [] //新增因子套餐ID列表
					//处理因子
					if (allFatactorIds.length == 0) {
						deleteMethodIds = oldFatactor
					} else {
						oldFatactor.map((item) => {
							if (!allFatactorIds.includes(item)) {
								deleteMethodIds.push(item)
							}
						})
						allFatactorIds.map((item) => {
							if (!oldFatactor.includes(item)) {
								addMethodIds.push(item)
							}
						})
					}
					//处理套餐
					if (allGroupIds.length == 0) {
						deleteGroupIds = oldGroup
					} else {
						oldGroup.map((item) => {
							if (!allGroupIds.includes(item)) {
								deleteGroupIds.push(item)
							}
						})
						allGroupArr.map((item) => {
							if (!oldGroup.includes(item.standardCode)) {
								groups.push({
									"id": item.standardCode,
									"subCheckStandardIds": item.subCheckStandardIds
								})
							}
						})
					}
					let params = {
						point: this.addressVal,
						sampleIds: this.sampsIdArr,
						deleteMethodIds: deleteGroupIds,
						deleteGroupIds: [],
						addMethodIds: addMethodIds,
						groups: groups
					}

					console.log(params, 'params');
					this.$api.commonForPost('sample/updateBySampleIds', params, res => {
						if (res.code === 200) {
							uni.showToast({
								title: '提交成功，数据审核中...',
								icon: 'none',
								duration: 2000
							});
							setTimeout(() => {
								uni.navigateBack({
									delta: 1
								})
								this.commitLoading = false
							}, 2000);
						} else {
							uni.showToast({
								title: res.message,
								icon: 'none',
								duration: 2000
							});
							this.commitLoading = false
						}
					})
				})



				// 获取删除的因子

				// 获取新增的因子

			},
			duplicateRemoval(Arr) {
				let result = [];
				let map = new Map();
				for (let item of Arr) {
					if (!map.has(item.checkStandardId)) {
						map.set(item.checkStandardId, item);
					};
				};
				result = [...map.values()];
				return result
			},
			//判断数组对象中是否有某个值
			findElem(arrayToSearch, attr, val) {
				//
				for (var i = 0; i < arrayToSearch.length; i++) {
					if (arrayToSearch[i][attr] == val) {
						return i; //返回当前索引值
					}
				}
				return -1;
			},
			goToHandWriting(sampItemId) {
				this.$common.navigateTo('/pages/handWriting/index?sampItemId=' + sampItemId);
			},
			toEgManage() {
				uni.setNavigationBarTitle({
					title: '样品管理'
				})
				this.hidden = true
				this.isEgManage = true

			},

		}
	}
</script>

<style scoped>
	uni-page-body {
		height: 100%;
	}

	.task-wrap {
		background: #F0F2FA;
		height: 100vh;
		display: flex;
		flex-direction: column;

	}

	.project-info {
		height: 280rpx;
		width: calc(100%-98rpx);
		background-color: #D6D8DF;
		padding-left: 49rpx;
		padding-right: 49rpx;
	}

	.sample-detail {
		margin-top: 20rpx;
		font-size: 25rpx;
		height: 32rpx;
		display: flex;
		align-items: center;

	}

	.sample-detail .title {
		color: #193566;
		font-weight: bolder;
		opacity: 0.8;
		display: inline-block;
		width: 140rpx;
		text-align: right;
	}

	.sample-detail .value {
		color: #193566;
		font-weight: 400;
		opacity: 0.7;
		width: 65%;
		display: inline-block;
		padding-left: 5%;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		display: inline-block;
	}

	.scroll-wrap {
		flex: 1;
		overflow: scroll;
		padding-bottom: 50rpx;
	}

	.tabContainer {
		height: auto;
	}

	.signature {
		position: absolute;
		top: 0;
		right: 0;
		width: 167rpx;
		height: 61rpx;
		color: #fff;
		background-color: #193566;
		border-radius: 19rpx 19rpx 19rpx 19rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 25rpx;
	}

	.signatureImg image {
		position: absolute;
		top: 0;
		right: 0;
		width: 61rpx;
		height: 61rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 25rpx;
	}

	.footerBtn {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
	}

	.btn {
		border-radius: 0;
		width: 100%;
		background: #2f3a89;
	}

	.footerBtns {
		display: flex;
		background: #d2dcf8;
		padding: 10rpx 0;
		box-shadow: -1px 0 1px #ccc;
	}

	.dialogBtns {
		position: absolute;
		top: -180rpx;
		left: 0;
		padding: 10rpx 20rpx;
		background: #d2dcf8;
		width: 100%;
		box-shadow: 1px 0px 1px #ccc;
		padding-top: 20rpx;
	}

	.dialogBtn {
		margin-bottom: 10rpx;
		width: 100%;
		padding: 0;
		font-size: 14px;
		height: 60rpx;
		color: #2f3a89;
		background: #ffda94;
	}

	.leftTabsBtn {
		width: 100%;
		border: 1px solid #eee;
		height: 60rpx;
		font-size: 14px;
		color: #2f3a89;
		background: #ffda94;
	}

	.rightTabsBtn {
		height: 60rpx;
		font-size: 14px;
		background: #2f3a89;
	}

	.rightTabsSingleBtn {
		width: 50%;
		color: #2f3a89;
		height: 60rpx;
		font-size: 14px;
		box-shadow: 1px 1px 3px #ccc;
	}

	.center_between {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.top_head {
		background: #d7dae1;
		margin-top: 8px;
		padding: 10px;
		border-radius: 8px;
	}

	.top_head_nav {
		display: flex;
		justify-content: space-between;
		align-items: center;

	}

	.content {
		flex: 1;
		text-align: right;
		color: #68696d;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	.sample-term-wrap {
		width: calc(100%-70rpx);
		display: flex;
		justify-content: center;
		flex-direction: column;
		/* align-items: center; */
		padding-left: 35rpx;
		padding-right: 35rpx;
	}

	.sample-term {
		/* width: 90%; */
		/* min-height:390rpx; */
		/* height:370rpx; */
		border-radius: 16rpx;
		background: #fff;
		padding: 20rpx;
		box-shadow: 0px 3px 3px 1px rgba(0, 0, 0, 0.16);
		margin-top: 20rpx;
	}

	.nav_btn {
		position: absolute;
		top: 16rpx;
		right: 16rpx;
	}

	.navSingleBtn {
		height: 50rpx;
		font-size: 12px;
		padding: 0 8px;
		background: #2f3a89;
		box-shadow: 1px 1px 3px #ccc;
	}
</style>
